/*
 * Germinus
 */
package org.germinus.telcoblocks.servicios.diagram.rcp.providers;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.diagram.core.providers.AbstractViewProvider;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.notation.View;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.AnunciosEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.AnunciosNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.ClickToDialEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.ClickToDialNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.DesvioEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.DesvioNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.EnlaceComentarioEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.EnlaceEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.FacturacionEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.FacturacionNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.IMEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.IMNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.LlamadasEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.LlamadasNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.PersonalizacionEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.PersonalizacionNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.SERVICIOSEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.ServicioGenEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.ServicioGenNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.TelefonoEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.TelefonoNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.WebEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.edit.parts.WebNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.rcp.part.TelcoblocksVisualIDRegistry;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.AnunciosNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.AnunciosViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.ClickToDialNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.ClickToDialViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.DesvioNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.DesvioViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.EnlaceComentarioViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.EnlaceViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.FacturacionNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.FacturacionViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.IMNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.IMViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.LlamadasNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.LlamadasViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.PersonalizacionNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.PersonalizacionViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.SERVICIOSViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.ServicioGenNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.ServicioGenViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.TelefonoNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.TelefonoViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.WebNombreViewFactory;
import org.germinus.telcoblocks.servicios.diagram.rcp.view.factories.WebViewFactory;

/**
 * @generated
 */
public class TelcoblocksViewProvider extends AbstractViewProvider {

	/**
	 * @generated
	 */
	protected Class getDiagramViewClass(IAdaptable semanticAdapter,
			String diagramKind) {
		EObject semanticElement = getSemanticElement(semanticAdapter);
		if (SERVICIOSEditPart.MODEL_ID.equals(diagramKind)
				&& TelcoblocksVisualIDRegistry
						.getDiagramVisualID(semanticElement) != -1) {
			return SERVICIOSViewFactory.class;
		}
		return null;
	}

	/**
	 * @generated
	 */
	protected Class getNodeViewClass(IAdaptable semanticAdapter,
			View containerView, String semanticHint) {
		if (containerView == null) {
			return null;
		}
		IElementType elementType = getSemanticElementType(semanticAdapter);
		EObject domainElement = getSemanticElement(semanticAdapter);
		int visualID;
		if (semanticHint == null) {
			// Semantic hint is not specified. Can be a result of call from CanonicalEditPolicy.
			// In this situation there should be NO elementType, visualID will be determined
			// by VisualIDRegistry.getNodeVisualID() for domainElement.
			if (elementType != null || domainElement == null) {
				return null;
			}
			visualID = TelcoblocksVisualIDRegistry.getNodeVisualID(
					containerView, domainElement);
		} else {
			visualID = TelcoblocksVisualIDRegistry.getVisualID(semanticHint);
			if (elementType != null) {
				// Semantic hint is specified together with element type.
				// Both parameters should describe exactly the same diagram element.
				// In addition we check that visualID returned by VisualIDRegistry.getNodeVisualID() for
				// domainElement (if specified) is the same as in element type.
				if (!TelcoblocksElementTypes.isKnownElementType(elementType)
						|| (!(elementType instanceof IHintedType))) {
					return null; // foreign element type
				}
				String elementTypeHint = ((IHintedType) elementType)
						.getSemanticHint();
				if (!semanticHint.equals(elementTypeHint)) {
					return null; // if semantic hint is specified it should be the same as in element type
				}
				if (domainElement != null
						&& visualID != TelcoblocksVisualIDRegistry
								.getNodeVisualID(containerView, domainElement)) {
					return null; // visual id for node EClass should match visual id from element type
				}
			} else {
				// Element type is not specified. Domain element should be present (except pure design elements).
				// This method is called with EObjectAdapter as parameter from:
				//   - ViewService.createNode(View container, EObject eObject, String type, PreferencesHint preferencesHint) 
				//   - generated ViewFactory.decorateView() for parent element
				if (!SERVICIOSEditPart.MODEL_ID
						.equals(TelcoblocksVisualIDRegistry
								.getModelID(containerView))) {
					return null; // foreign diagram
				}
				switch (visualID) {
				case ClickToDialEditPart.VISUAL_ID:
				case LlamadasEditPart.VISUAL_ID:
				case ServicioGenEditPart.VISUAL_ID:
				case PersonalizacionEditPart.VISUAL_ID:
				case IMEditPart.VISUAL_ID:
				case FacturacionEditPart.VISUAL_ID:
				case WebEditPart.VISUAL_ID:
				case DesvioEditPart.VISUAL_ID:
				case AnunciosEditPart.VISUAL_ID:
				case TelefonoEditPart.VISUAL_ID:
					if (domainElement == null
							|| visualID != TelcoblocksVisualIDRegistry
									.getNodeVisualID(containerView,
											domainElement)) {
						return null; // visual id in semantic hint should match visual id for domain element
					}
					break;
				case ClickToDialNombreEditPart.VISUAL_ID:
					if (ClickToDialEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case LlamadasNombreEditPart.VISUAL_ID:
					if (LlamadasEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case ServicioGenNombreEditPart.VISUAL_ID:
					if (ServicioGenEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case PersonalizacionNombreEditPart.VISUAL_ID:
					if (PersonalizacionEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case IMNombreEditPart.VISUAL_ID:
					if (IMEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case FacturacionNombreEditPart.VISUAL_ID:
					if (FacturacionEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case WebNombreEditPart.VISUAL_ID:
					if (WebEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case DesvioNombreEditPart.VISUAL_ID:
					if (DesvioEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case AnunciosNombreEditPart.VISUAL_ID:
					if (AnunciosEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case TelefonoNombreEditPart.VISUAL_ID:
					if (TelefonoEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				case EnlaceComentarioEditPart.VISUAL_ID:
					if (EnlaceEditPart.VISUAL_ID != TelcoblocksVisualIDRegistry
							.getVisualID(containerView)
							|| containerView.getElement() != domainElement) {
						return null; // wrong container
					}
					break;
				default:
					return null;
				}
			}
		}
		return getNodeViewClass(containerView, visualID);
	}

	/**
	 * @generated
	 */
	protected Class getNodeViewClass(View containerView, int visualID) {
		if (containerView == null
				|| !TelcoblocksVisualIDRegistry.canCreateNode(containerView,
						visualID)) {
			return null;
		}
		switch (visualID) {
		case ClickToDialEditPart.VISUAL_ID:
			return ClickToDialViewFactory.class;
		case ClickToDialNombreEditPart.VISUAL_ID:
			return ClickToDialNombreViewFactory.class;
		case LlamadasEditPart.VISUAL_ID:
			return LlamadasViewFactory.class;
		case LlamadasNombreEditPart.VISUAL_ID:
			return LlamadasNombreViewFactory.class;
		case ServicioGenEditPart.VISUAL_ID:
			return ServicioGenViewFactory.class;
		case ServicioGenNombreEditPart.VISUAL_ID:
			return ServicioGenNombreViewFactory.class;
		case PersonalizacionEditPart.VISUAL_ID:
			return PersonalizacionViewFactory.class;
		case PersonalizacionNombreEditPart.VISUAL_ID:
			return PersonalizacionNombreViewFactory.class;
		case IMEditPart.VISUAL_ID:
			return IMViewFactory.class;
		case IMNombreEditPart.VISUAL_ID:
			return IMNombreViewFactory.class;
		case FacturacionEditPart.VISUAL_ID:
			return FacturacionViewFactory.class;
		case FacturacionNombreEditPart.VISUAL_ID:
			return FacturacionNombreViewFactory.class;
		case WebEditPart.VISUAL_ID:
			return WebViewFactory.class;
		case WebNombreEditPart.VISUAL_ID:
			return WebNombreViewFactory.class;
		case DesvioEditPart.VISUAL_ID:
			return DesvioViewFactory.class;
		case DesvioNombreEditPart.VISUAL_ID:
			return DesvioNombreViewFactory.class;
		case AnunciosEditPart.VISUAL_ID:
			return AnunciosViewFactory.class;
		case AnunciosNombreEditPart.VISUAL_ID:
			return AnunciosNombreViewFactory.class;
		case TelefonoEditPart.VISUAL_ID:
			return TelefonoViewFactory.class;
		case TelefonoNombreEditPart.VISUAL_ID:
			return TelefonoNombreViewFactory.class;
		case EnlaceComentarioEditPart.VISUAL_ID:
			return EnlaceComentarioViewFactory.class;
		}
		return null;
	}

	/**
	 * @generated
	 */
	protected Class getEdgeViewClass(IAdaptable semanticAdapter,
			View containerView, String semanticHint) {
		IElementType elementType = getSemanticElementType(semanticAdapter);
		if (!TelcoblocksElementTypes.isKnownElementType(elementType)
				|| (!(elementType instanceof IHintedType))) {
			return null; // foreign element type
		}
		String elementTypeHint = ((IHintedType) elementType).getSemanticHint();
		if (elementTypeHint == null) {
			return null; // our hint is visual id and must be specified
		}
		if (semanticHint != null && !semanticHint.equals(elementTypeHint)) {
			return null; // if semantic hint is specified it should be the same as in element type
		}
		int visualID = TelcoblocksVisualIDRegistry.getVisualID(elementTypeHint);
		EObject domainElement = getSemanticElement(semanticAdapter);
		if (domainElement != null
				&& visualID != TelcoblocksVisualIDRegistry
						.getLinkWithClassVisualID(domainElement)) {
			return null; // visual id for link EClass should match visual id from element type
		}
		return getEdgeViewClass(visualID);
	}

	/**
	 * @generated
	 */
	protected Class getEdgeViewClass(int visualID) {
		switch (visualID) {
		case EnlaceEditPart.VISUAL_ID:
			return EnlaceViewFactory.class;
		}
		return null;
	}

	/**
	 * @generated
	 */
	private IElementType getSemanticElementType(IAdaptable semanticAdapter) {
		if (semanticAdapter == null) {
			return null;
		}
		return (IElementType) semanticAdapter.getAdapter(IElementType.class);
	}
}
